home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Digital Signatures / Digital Signature Demo / Source ƒ / DemoApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  6.6 KB  |  291 lines  |  [TEXT/KAHL]

  1. /*
  2.  * DemoApp.c
  3.  *    Copyright © 1992-93 Apple Computer Inc. All rights reserved.
  4.  * This sub-class to CEditApp adds Digital Signature
  5.  * capabilities to the sample editor.
  6.  */
  7. #include "DemoApp.h"
  8. #include "DemoEditDoc.h"
  9. #include "DemoSignedObject.h"
  10. #include "DemoSignedObjectDialog.h"
  11. #include <Commands.h>
  12. #include <CBartender.h>
  13. #include <CList.h>
  14. #include <Global.h>
  15. #include <Exceptions.h>
  16. #include <Errors.h>
  17. extern CApplication            *gApplication;
  18. extern CBartender            *gBartender;
  19. extern CursHandle            gWatchCursor;
  20. extern CBureaucrat            *gGopher;
  21. extern    OSType                gSignature;
  22. Boolean                        gHasDigitalSignatureManager;
  23.  
  24. OSErr                        SystemSupportsAOCE(void);
  25. void                        Message(
  26.         const StringPtr            textString
  27.     );
  28.  
  29. /*
  30.  * IDemoApp
  31.  * Just call the CEditApp initializer.
  32.  */
  33. void
  34. DemoApp::IDemoApp(void)
  35. {
  36.         gHasDigitalSignatureManager = (SystemSupportsAOCE() == noErr);
  37.         CEditApp::IEditApp();
  38.         if (gHasDigitalSignatureManager == FALSE) {
  39.             Message(
  40.                 "\pYour system does not support the Digital Signature"
  41.                 " Manager. Some menu items have been disabled for"
  42.                 " your convenience."
  43.             );
  44.         }
  45.         itsDemoSignedObject = new (DemoSignedObject);
  46.         itsDemoSignedObject->IDemoSignedObject();
  47. }
  48.  
  49. void
  50. DemoApp::SetUpFileParameters(void)
  51. {
  52.         inherited::SetUpFileParameters();
  53.         sfNumTypes = 2;
  54.         sfFileTypes[0] = 'TEXT';
  55.         sfFileTypes[1] = 'ttro';            /* TeachText read-only            */
  56.         gSignature = 'ttxt';                /* TeachText                    */
  57. }
  58.  
  59.  
  60. /*
  61.  * This is called by a patch to ExitToShell whenever the application
  62.  * exits, even if it dies a horrible death. This ensures that the
  63.  * Digital Signature Manager's context is cleared. If we don't do
  64.  * this, the application will fail when it is restarted.
  65.  */
  66. void
  67. DemoApp::RemovePatches(void)
  68. {
  69.         DisposeSignerContext();
  70.         inherited::RemovePatches();
  71. }
  72.  
  73. /*
  74.  * SetUpMenus
  75.  * Our special menus have already been added to the
  76.  * resource file. Just call CEditDoc's method.
  77.  */
  78. void
  79. DemoApp::SetUpMenus(void)
  80. {
  81.         inherited::SetUpMenus();
  82. }
  83.  
  84. /*
  85.  * DoCommand
  86.  * Process our special commands. OpenSignedFile was added
  87.  * to the File Menu by the SetUpMenus method.
  88.  * Note that we have to override the "new document" method
  89.  * in order to create a signable document.
  90.  */
  91. void
  92. DemoApp::DoCommand(
  93.         long                theCommand
  94.     )
  95. {
  96.         SFReply                    macSFReply;
  97.         
  98.         switch (theCommand) {
  99.         case cmdDebugTrapOnError:
  100.             /*
  101.              * Enable/disable a Debug trap if an
  102.              * error is detected. This might reasonably
  103.              * be enabled if you are running under the
  104.              * Think C debugger.
  105.              */
  106.             gBreakFailure = !(gBreakFailure);
  107.             gBartender->CheckMarkCmd(
  108.                             cmdDebugTrapOnError,
  109.                             gBreakFailure
  110.                         );
  111.             break;
  112.         case cmdOpenSignedFile:
  113.             ChooseFile(&macSFReply);
  114.             if (macSFReply.good) {
  115.                 SetCursor(*gWatchCursor);
  116.                 OpenSignedDocument(&macSFReply);
  117.             }
  118.             break;
  119.         case cmdPrint:
  120.             inherited::DoCommand(theCommand);
  121.             break;
  122.         case cmdOpen:
  123.             /*
  124.              * We override cmdOpen here to detect the "trying to open
  125.              * a read-only file" error that occurs if the user tries
  126.              * to open a signed file using File/Open.
  127.              */
  128.             TRY {
  129.                 inherited::DoCommand(theCommand);
  130.             }
  131.             CATCH {
  132.                 if (gLastError == afpAccessDenied) {
  133.                     Message(
  134.                         "\pThis file was set “locked” (i.e.,"
  135.                         " read-only) by the Finder or by signing"
  136.                         " the file. If it was signed, you can use"
  137.                         " Open and Verify File to open it read-only."
  138.                     );
  139.                     gApplication->JumpToEventLoop();
  140.                 }
  141.             }
  142.             ENDTRY;
  143.             break;
  144.         case cmdNew:
  145.             SetCursor(*gWatchCursor);
  146.             CreateSignedDocument();
  147.             break;
  148.         case cmdSignedObjectDemo:
  149.             /*
  150.              * The user chose "Signed Object Demo."
  151.              * -- we treat this as a modal dialog, even though
  152.              * there's no real reason to. itsDemoSignedObjectDialog
  153.              * is in the application object instance so it is
  154.              * globalized for debugging: it could just as well
  155.              * be a local variable as it has no existance outside
  156.              * of this code sequence.
  157.              */
  158.             itsDemoSignedObjectDialog = new (DemoSignedObjectDialog);
  159.             TRY {
  160.                 itsDemoSignedObjectDialog->IDemoSignedObjectDialog(
  161.                     this,
  162.                     itsDemoSignedObject
  163.                 );
  164.                 itsDemoSignedObjectDialog->DoModalDialog(cmdOK);
  165.             }
  166.             CATCH {
  167.                 ForgetObject(itsDemoSignedObjectDialog);
  168.             }
  169.             ENDTRY;
  170.             ForgetObject(itsDemoSignedObjectDialog);
  171.             break;
  172.         case cmdAbout:
  173.             if (gHasDigitalSignatureManager) {
  174.                 Message(
  175.                     "\pDigital Signature Sample."
  176.                     " Copyright © 1993 Apple Computer Inc."
  177.                     " All Rights Reserved."
  178.                 );
  179.             }
  180.             else {
  181.                 Message(
  182.                     "\pDigital Signature Sample."
  183.                     " Copyright © 1993 Apple Computer Inc."
  184.                     " All Rights Reserved."
  185.                     " Note: this system does not support"
  186.                     " the Digital Signature Manager."
  187.                 );
  188.             }
  189.             break;
  190.         default:
  191.             inherited::DoCommand(theCommand);
  192.             break;
  193.         }
  194. }
  195.  
  196. /*
  197.  * UpdateMenus 
  198.  */
  199. void
  200. DemoApp::UpdateMenus(void)
  201. {
  202.          inherited::UpdateMenus();     /* Enable standard commands */      
  203.         /*
  204.          * Allow opening existing (unsigned) text files,
  205.          * and allow opening (with verification) of signed text files.
  206.          */
  207.         gBartender->EnableCmd(cmdOpen);
  208.         gBartender->DisableCmd(cmdRevert);
  209.         gBartender->EnableMenu(MENU_ObjectDemo);
  210.         gBartender->EnableCmd(cmdSignedObjectDemo);
  211.         gBartender->EnableCmd(cmdDebugTrapOnError);
  212.         /*
  213.          * If we have the Digital Signature Manager and there is a
  214.          * document attached, enable the "Close and Sign File" menu item.
  215.          */
  216.         if (gHasDigitalSignatureManager) {
  217.             gBartender->EnableCmd(cmdOpenSignedFile);
  218.             if (itsDirectors != NULL
  219.              && itsDirectors->GetNumItems() > 0)
  220.                 gBartender->EnableCmd(cmdCloseAndSignFile);
  221.         }
  222.  }
  223.  
  224. /*
  225.  * Override CEditApp::OpenDocument to enable the "save,
  226.  * close, and sign" menu option.
  227.  */
  228. void
  229. DemoApp::OpenDocument(
  230.         SFReply                    *macSFReply
  231.     )
  232. {
  233.         DemoEditDoc    *theDocument = NULL;
  234.  
  235.         TRY {    
  236.             theDocument = new(DemoEditDoc);
  237.             theDocument->IDemoEditDoc(this, TRUE);
  238.             theDocument->OpenFile(macSFReply);
  239.         }
  240.         CATCH {
  241.             ForgetObject( theDocument);
  242.         }
  243.         ENDTRY;
  244. }
  245. /*
  246.  * OpenSignedDocument
  247.  * This is called when the user chooses Open and Verify Document
  248.  * from the file menu. 
  249.  */
  250. void
  251. DemoApp::OpenSignedDocument(
  252.         SFReply                    *macSFReply
  253.     )
  254. {
  255.         DemoEditDoc    *theDocument = NULL;
  256.         
  257.         TRY {
  258.             theDocument = new(DemoEditDoc);
  259.             theDocument->IDemoEditDoc(this, TRUE);
  260.             theDocument->OpenAndVerifyFile(macSFReply);
  261.         } 
  262.         CATCH {
  263.             ForgetObject(theDocument);
  264.         }
  265.         ENDTRY;
  266. }
  267.  
  268. /*
  269.  * CreateSignedDocument
  270.  * This is called when the user chooses New from the
  271.  * file menu. It is identical to CreateDocument in
  272.  * CEditApp, except that it creates a signable document.
  273.  */
  274. void
  275. DemoApp::CreateSignedDocument(void)
  276. {
  277.         DemoEditDoc    *theDocument = NULL;
  278.         
  279.         TRY {
  280.             theDocument = new(DemoEditDoc);
  281.             theDocument->IDemoEditDoc(this, TRUE);
  282.             theDocument->NewFile();
  283.         }
  284.         CATCH {
  285.             ForgetObject(theDocument);
  286.         }
  287.         ENDTRY;
  288. }
  289.  
  290.  
  291.